package com.imoblife.brainwavestus;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;

import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;

import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class BrainWavestusApplication extends Application {

	private boolean mIsPad;
	private boolean mIsFirstLaunch = true;
	private boolean mIsLoadFromXmlFile = true;
	private int mNumOfCpu;
	private Store mStore;
	private User mUser;
	
	public BrainWavestusApplication() {
		
		super();
	}
	
	public int getNumOfCpu() {
			
		return mNumOfCpu;
	}
	
	public Store getStore() {
		
		return mStore;
	}
	
	public void updateStore(Store newStore) {
		
		mStore = newStore;
	}
	
	public boolean isPad() {
		return mIsPad;
	}
	
	public boolean isFirstLaunch() {
		
		return mIsFirstLaunch;
	}
	
	@Override
	public void onCreate() {		
		super.onCreate();
		
		mIsPad = checkIsPad();	
		mStore = new Store();
		mUser = new User();	
		mNumOfCpu = getNumCores();
	}
	
	private int getNumCores() {

		// Private Class to display only CPU devices in the directory listing

		class CpuFilter implements FileFilter {
			public boolean accept(File pathname) {
				// Check if filename is "cpu", followed by a single digit number
				if (Pattern.matches("cpu[0-9]", pathname.getName())) {
					return true;
				}
				return false;
			}
		}

		try {
			// Get directory containing CPU info
			File dir = new File("/sys/devices/system/cpu/");
			// Filter to only list the devices we care about
			File[] files = dir.listFiles(new CpuFilter());
			// Return the number of cores (virtual CPU devices)
			return files.length;
		} catch (Exception e) {
			// Default to return 1 core
			return 1;
		}
	}

	public void setIsFirstLaunch(boolean isFirstLaunch){
		
		mIsFirstLaunch = isFirstLaunch;
	}
	/**
	 * 判断是否为平板
	 * 
	 * @return
	 */
	private boolean checkIsPad() {
		WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		// 屏幕宽度
		float screenWidth = display.getWidth();
		// 屏幕高度
		float screenHeight = display.getHeight();
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
		double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
		// 屏幕尺寸
		double screenInches = Math.sqrt(x + y);
		// 大于6尺寸则为Pad
		if (screenInches >= 6.0) {
			return true;
		}
		return false;
	}

	public User getUser() {
		return mUser;
	}

	public void setUser(User mUser) {
		this.mUser = mUser;
	}

	public boolean ismIsLoadFromXmlFile() {
		return mIsLoadFromXmlFile;
	}

	public void setmIsLoadFromXmlFile(boolean mIsLoadFromXmlFile) {
		this.mIsLoadFromXmlFile = mIsLoadFromXmlFile;
	}
}
